package com.dxf.hchat.service.impl;

import com.dxf.hchat.mapper.TbChatRecordMapper;
import com.dxf.hchat.pojo.TbChatRecord;
import com.dxf.hchat.pojo.TbChatRecordExample;
import com.dxf.hchat.service.ChatRecordService;
import com.dxf.hchat.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ChatRecordServiceImpl implements ChatRecordService {
    @Autowired
    private TbChatRecordMapper tbChatRecordMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void insert(TbChatRecord chatRecord) {

        chatRecord.setId(idWorker.nextId());
        chatRecord.setCreatetime(new Date());
        chatRecord.setHasDelete(0);
        chatRecord.setHasRead(0);
        tbChatRecordMapper.insert(chatRecord);
    }

    /**
     * 根据用户id和朋友id查询聊天记录
     * @param userid
     * @param friendId
     * @return
     */
    @Override
    public List<TbChatRecord> findByUserIdAndFriendId(String userid, String friendId) {

        TbChatRecordExample example = new TbChatRecordExample();
        TbChatRecordExample.Criteria criteria1 = example.createCriteria();
        criteria1.andUseridEqualTo(userid);
        criteria1.andFriendidEqualTo(friendId);
        criteria1.andHasDeleteEqualTo(0);//未删除
        criteria1.andHasReadEqualTo(0);//未读

        TbChatRecordExample.Criteria criteria2 = example.createCriteria();
        criteria2.andUseridEqualTo(friendId);
        criteria2.andFriendidEqualTo(userid);
        criteria2.andHasReadEqualTo(0);//未读
        criteria2.andHasDeleteEqualTo(0);//未删除

        example.or(criteria1);
        example.or(criteria2);

        List<TbChatRecord> list = tbChatRecordMapper.selectByExample(example);

        //把所有未读消息改为已读
        TbChatRecordExample exampleQueryRead = new TbChatRecordExample();
        TbChatRecordExample.Criteria criteria = exampleQueryRead.createCriteria();
        criteria.andUseridEqualTo(friendId);
        criteria.andHasReadEqualTo(0);
        List<TbChatRecord> tbChatRecords = tbChatRecordMapper.selectByExample(exampleQueryRead);
        for (TbChatRecord tbChatRecord : tbChatRecords) {
            tbChatRecord.setHasRead(1);
            tbChatRecordMapper.updateByPrimaryKey(tbChatRecord);
        }

        return list;

    }


    /**
     * 查找未读聊天记录
     */
    @Override
    public List<TbChatRecord> findUnreadByUserid(String userid) {

        TbChatRecordExample example = new TbChatRecordExample();
        TbChatRecordExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(userid);
        criteria.andHasDeleteEqualTo(0);
        criteria.andHasReadEqualTo(0);

        return tbChatRecordMapper.selectByExample(example);

    }

    /**
     * 更新数据库状态已读
     * @param id
     */
    @Override
    public void updateStatusHasRead(String id) {

        TbChatRecord tbChatRecord = tbChatRecordMapper.selectByPrimaryKey(id);
        tbChatRecord.setHasRead(1);
        tbChatRecordMapper.updateByPrimaryKey(tbChatRecord);

    }
}
